using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

public partial class UserControls_PicturesList : System.Web.UI.UserControl
{
    string galleryId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
        }
    }

    private void PopulateControls()
    {
        // Retrieve GalleryID from the query string
        galleryId = Request.QueryString["GalleryID"];
        // Retrieve Page from the query string
        string page = Request.QueryString["Page"];
        if (page == null) page = "1";
        // How many pages of products?
        int howManyPages = 1;

        if (Int32.Parse(galleryId) == 1)
        {
            gallerylabel.Text = "<b>Galeria - Sklep</b>";
            list.Visible = true;
            list.Enabled = true;
            list.DataSource = PhotoGallery.GetImagesInGallery(galleryId, page, out howManyPages);
            list.DataBind();
        }

        if (Int32.Parse(galleryId) == 2)
        {
            gallerylabel.Text = "<b>Galeria - Nasi Klienci</b>";
            list2.Visible = true;
            list2.Enabled = true;
            list2.DataSource = PhotoGallery.GetImagesInGallery(galleryId, page, out howManyPages);
            list2.DataBind();
        }

        // display paging controls 
        if (howManyPages > 1)
        {
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            // make controls visible
            pagingLabel.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;
            // set the paging text
            pagingLabel.Text = "Strona " + page + " z " + howManyPages.ToString();
            // create the Previous link
            if (currentPage == 1)
            {
                previousLink.Enabled = false;
                previousLink.Visible = false;
            }
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                previousLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString
                + "Page=" + (currentPage - 1).ToString();
            }
            // create the Next link
            if (currentPage == howManyPages)
            {
                nextLink.Enabled = false;
                nextLink.Visible = false;
            }
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                nextLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString +
                "Page=" + (currentPage + 1).ToString();
            }
        }
    }
}
